package sample;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.Background;
import io.reactivex.Observable;
import io.reactivex.Observer;

import javax.swing.*;
import java.util.ArrayList;

public class ProgressBarThread extends JFrame implements Runnable{
    JProgressBar progressBar; // 进度条
    ProgressIndicator ploading;
    private int progress;//当前进度
    private int totalSize;//总大小
    private boolean run = true;

    public ProgressBarThread(String str) {
        super(str);
        progressBar = new JProgressBar(1, 100); // 实例化进度条
        ploading = new ProgressIndicator();
        ploading.setBackground(Background.EMPTY);
        progressBar.setStringPainted(true);// 描绘文字
        progressBar.setBackground(java.awt.Color.PINK); // 设置背景色
        this.add(progressBar);
        this.setBounds(200, 300, 250, 80);
        this.setResizable(false);
    }

    public void setVisible() {
        this.setVisible(true);
    }

    public ProgressBarThread(int totalSize){
        this.totalSize = totalSize;
        //TODO 创建进度条
    }

    /**
     * @param progress 进度
     */
    public void updateProgressBar(int progress){
           this.progress=progress;
    }


    @Override
    public void run() {
            try {
                while (this.run) {
                        //TODO 更新进度条
                       progressBar.setValue(progress); // 随着线程进行，增加进度条值
                       progressBar.setString(progressBar.getValue() + "%");
                       if(progressBar.getValue()==100)
                       {
                           JOptionPane.showMessageDialog(this, "下载完成!");
                           this.dispose();
                           break;
                       }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}
